﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using DevExpress.XtraLayout.Utils;

namespace Sfcorp.form
{
    public partial class frmViewBnsImage : DevExpress.XtraEditors.XtraForm
    {
        byte[] img = null;
        Bitmap bmpImage = null;
        Timer tmr;

        public frmViewBnsImage(byte[] _img)
        {
            InitializeComponent();
            img = _img;
        }

        int GetZoomValue()
        {
            if (zoomTrackBarControl1.Value <= 40)
                return 10 + 90 * (zoomTrackBarControl1.Value - 0) / 40;
            else
                return 100 + 400 * (zoomTrackBarControl1.Value - 40) / 40;
        }
        int ZoomValueToValue(int zoomValue)
        {
            if (zoomValue < 100)
                return Math.Min(80, Math.Max(0, (zoomValue - 10) * 40 / 90));
            return Math.Min(80, Math.Max(0, (zoomValue - 100) * 40 / 400 + 40));
        }
        bool updatedZoom = false;
        private void zoomTrackBarControl1_EditValueChanged(object sender, EventArgs e)
        {
            if (updatedZoom) return;
            updatedZoom = true;
            try
            {
                zoomTextEdit.EditValue = GetZoomValue();
                //ZoomFactor = (float) Convert.ToDouble(zoomTextEdit.EditValue);
                ZoomFactor = (float)zoomTextEdit.Value/100;
            }
            finally
            {
                updatedZoom = false;
            }
        }

        private void frmViewBnsImage_Load(object sender, EventArgs e)
        {

            MemoryStream ms1 = new MemoryStream(img);
            Bitmap bmpImage = (Bitmap)Image.FromStream(ms1);
            Point ptLoction = new Point(bmpImage.Size);

            pictureEditZoom1.EditValue = img;

            if (ptLoction.X > pictureEditZoom1.Size.Width || ptLoction.Y > pictureEditZoom1.Size.Height)
            {
                //图像框的停靠体式格式  
                //图像充滿图像框，並且图像維持比例  
                pictureEditZoom1.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Zoom;
            }
            else
            {
                //图像在图像框置中  
                pictureEditZoom1.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Stretch;
                pictureEditZoom1.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Clip;
                pictureEditZoom1.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Squeeze;
            }
        }

        //---------------------------------
        Timer PictureTimer
        {
            get
            {
                if (tmr == null)
                {
                    tmr = new Timer();
                    tmr.Interval = 100;
                    tmr.Enabled = false;
                    tmr.Tick += new EventHandler(tmr_Tick);
                }
                return tmr;
            }
        }
        void tmr_Tick(object sender, EventArgs e)
        {
            SizePhoto();
            PictureTimer.Stop();
        }
        private void pictureEditZoom1_Resize(object sender, EventArgs e)
        {
            PictureTimer.Start();
        }
        void ucContactInfo_SizeChanged(object sender, EventArgs e)
        {
            PictureTimer.Start();
        }
        bool lockResize = false;
        void SizePhoto()
        {
            if (lockResize) return;
            lockResize = true;
            Image img = pictureEditZoom1.Image;
            try
            {
                int height = img.Height * pictureEditZoom1.Properties.ZoomPercent / 100 + 4;
                if (pictureEditZoom1.Properties.ZoomPercent == 100)
                    pictureEditZoom1.Height = Math.Min(pictureEditZoom1.Width * img.Height / img.Width, height);
                else pictureEditZoom1.Height = height;
            }
            finally
            {
                lockResize = false;
            }
        }

        public float ZoomFactor
        {
            get { return (float)pictureEditZoom1.Properties.ZoomPercent / 100; }
            set
            {
                if (value == 1)
                {
                    pictureEditZoom1.Properties.ZoomPercent = 100;
                    pictureEditZoom1.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Squeeze;
                    pictureEditZoom1.Properties.ShowScrollBars = false;
                }
                else
                {
                    pictureEditZoom1.Properties.ZoomPercent = (int)(value * 100);
                    pictureEditZoom1.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Clip;
                    pictureEditZoom1.Properties.ShowScrollBars = true;
                }
                SizePhoto();
            }
        }
    }
}